// pages/recruit/personal/mytask/mytasksign/mytasksign.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageindex: 3,
    candidate: [],
    hiringList: [],
    selectbox: [],
    allcheck: false,
    accountList: [],
    jobid: '111111'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    console.log(options.jobid)
    let candidate = [{
        userid: "id1",
        name: "王大拿",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "16874",
        ischecked: false,
        ishired: false,
      },
      {
        userid: "id2",
        name: "安安",
        sex: 2,
        age: 27,
        tel: "17755015130",
        education: 2,
        jobid: "544",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id3",
        name: "牛逼",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: false,
        ishired: false,
      },
      {
        userid: "id4",
        name: "牛逼1",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: false,
        ishired: false,
      },
      {
        userid: "id5",
        name: "牛逼2",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: false,
        ishired: false,
      }
    ];
    let hiringList = [{
        userid: "id1",
        name: "王大拿",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "16874",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id2",
        name: "安安",
        sex: 2,
        age: 27,
        tel: "17755015130",
        education: 2,
        jobid: "544",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id3",
        name: "牛逼",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id4",
        name: "牛逼1",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id5",
        name: "牛逼2",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      }
    ];
    let accountList = [
      {
        userid: "id1",
        name: "王大拿",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "16874",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id2",
        name: "安安",
        sex: 2,
        age: 27,
        tel: "17755015130",
        education: 2,
        jobid: "544",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id3",
        name: "牛逼",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id4",
        name: "牛逼1",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      },
      {
        userid: "id5",
        name: "牛逼2",
        sex: 1,
        age: 27,
        tel: "17755015130",
        education: 1,
        jobid: "14656",
        ischecked: true,
        ishired: true,
      }
    ]
    that.setData({
      candidate: candidate,
      hiringList: hiringList,
      accountList: accountList
    });
  },
  changeindex: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      pageindex: index
    })
  },
  getgroup: function(e) {
    const that = this;
    let candidatelist = e.detail.value;
    let box = that.data.candidate;
    let newarr = [];
    if (candidatelist.length != box.length) {
      that.data.allcheck = false;
    } else {
      that.data.allcheck = true;
    }
    console.log(box)
    for (let i = 0; i < candidatelist.length; i++) {
      newarr.push({
        "userid": box[candidatelist[i]].userid,
        "jobid": box[candidatelist[i]].jobid
      })
      for (let j = 0; j < box.length; j++) {
        if (candidatelist[i].userid != box[j].userid) {
          box[j].ischecked = false;
        }
      }
    }
    that.setData({
      allcheck: that.data.allcheck,
      selectbox: newarr
    })
  },
  checkall: function(e) {
    const that = this;
    let box = that.data.candidate;
    if (!that.data.allcheck) {
      for (let i = 0; i < box.length; i++) {
        if (!box[i].ishired) {
          box[i].ischecked = true;
          that.data.selectbox.push({
            "userid": box[i].userid,
            "jobid": box[i].jobid
          })
        }
      }
      that.setData({
        allcheck: true,
      })
    } else {
      for (let i = 0; i < box.length; i++) {
        box[i].ischecked = false;
        that.data.selectbox = [];
      }
      that.setData({
        allcheck: false,
      })
    }
    that.setData({
      candidate: that.data.candidate,
      selectbox: that.data.selectbox
    })
  },
  employ: function() {
    const that = this;
    let box = that.data.candidate;
    let selectbox = that.data.selectbox;;
    console.log(selectbox)
    if (selectbox.length == 0) {
      wx.showToast({
        title: '你还未选中应聘者',
        icon: 'none',
        duration: 1500
      })
    } else {
      for (let i = 0; i < box.length; i++) { //模拟请求后的数据处理，选中后被录取就不会在显示，并且selectbox也会清空
        for (let j = 0; j < selectbox.length; j++) {
          if (selectbox[j].userid == box[i].userid) {
            console.log(box[i])
            box[i].ishired = true;
            box[i].ischecked = true;
          }
        }
      }
      that.setData({
        candidate: that.data.candidate,
        selectbox: [],
      })
      console.log(that.data.candidate)
    }
  },
  attendance:function(e) {
    let jobid = this.data.jobid;
    wx.navigateTo({
      url: '/pages/recruit/personal/mytask/mytasksign/attendance/attendance?jobid=' + jobid,
    })
  },
  account: function(e){
    console.log("结算")
  }
})